import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:largefriends/entity/entities/lf_user_entity.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/lf_fonts.dart';
import 'package:largefriends/res/lf_text_style.dart';
import 'package:largefriends/widgets/custom_cache_image.dart';

import '../res.dart';
import '../res/lf_widget_ext.dart';

class UserCardWidget extends StatelessWidget {
  final LfUserEntity userEntity;
  final bool needBlur;
  final bool showNew;
  const UserCardWidget(
      {Key? key,
      required this.userEntity,
      this.needBlur = false,
      this.showNew = false})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: ScreenUtil().screenWidth,
      height: ScreenUtil().screenWidth,
      child: Stack(
        children: [
          Positioned(
            child: ClipRRect(
              borderRadius: 16.r.radius,
              child: ImageFiltered(
                enabled: needBlur,
                imageFilter: ImageFilter.blur(sigmaX: 20, sigmaY: 20),
                child: CustomCacheImage(
                  imageUrl: userEntity.avatar,
                  width: ScreenUtil().screenWidth,
                  height: ScreenUtil().screenWidth,
                  radius: 16.r,
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.bottomCenter,
            child: IntrinsicHeight(
              child: ClipRRect(
                borderRadius: BorderRadius.only(
                    bottomRight: Radius.circular(16.r),
                    bottomLeft: Radius.circular(16.r)),
                child: BackdropFilter(
                  filter: ImageFilter.blur(sigmaX: 20, sigmaY: 20),
                  child: Container(
                    color: _bottomColor(),
                    padding:
                        EdgeInsets.symmetric(vertical: 10.h, horizontal: 10.w),
                    constraints: BoxConstraints(
                        minWidth: ScreenUtil().screenWidth,
                        maxWidth: ScreenUtil().screenWidth,
                        minHeight: 60.h),
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Row(
                          children: [
                            Flexible(
                                child: needBlur
                                    ? ClipRRect(
                                        borderRadius: 30.r.radius,
                                        child: ImageFiltered(
                                          imageFilter: ImageFilter.blur(
                                              sigmaX: 20, sigmaY: 20),
                                          enabled: true,
                                          child: Container(
                                            decoration: BoxDecoration(
                                                color: colorFEFEFE),
                                            child: Text(
                                              userEntity
                                                  .getNameWithGenderAge()
                                                  .toUpperCase(),
                                              maxLines: 1,
                                              overflow: TextOverflow.ellipsis,
                                              style: lfTextStyle(
                                                  sp: 15.sp,
                                                  fontFamily: fontBold,
                                                  color: Colors.white),
                                            ),
                                          ),
                                        ),
                                      )
                                    : Text(
                                        userEntity
                                            .getNameWithGenderAge()
                                            .toUpperCase(),
                                        maxLines: 1,
                                        overflow: TextOverflow.ellipsis,
                                        textWidthBasis:
                                            TextWidthBasis.longestLine,
                                        style: lfTextStyle(
                                            sp: 15.sp,
                                            fontFamily: fontBold,
                                            color: Colors.white),
                                      )),
                            userEntity.isVerified()
                                ? Padding(
                                    padding: EdgeInsets.only(left: 6.w),
                                    child: Image.asset(Res.ic_verified,
                                        width: 16.r),
                                  )
                                : empty,
                            userEntity.isGold()
                                ? Padding(
                                    padding: EdgeInsets.only(left: 6.w),
                                    child: Image.asset(
                                      Res.icon_premium,
                                      width: 18.r,
                                      color: colorFF007F,
                                    ),
                                  )
                                : empty,
                            userEntity.isOnline() ||
                                    userEntity.isOnlineRecently()
                                ? Container(
                                    width: 10.r,
                                    height: 10.r,
                                    margin: EdgeInsets.only(
                                      left: 6.w,
                                    ),
                                    decoration: const BoxDecoration(
                                        color: Colors.green,
                                        shape: BoxShape.circle),
                                  )
                                : empty
                          ],
                        ),
                        userEntity.getLiveIn().isEmpty ? empty : 6.h.sbh,
                        userEntity.getLiveIn().isEmpty
                            ? empty
                            : needBlur
                                ? ClipRRect(
                                    borderRadius: 30.r.radius,
                                    child: ImageFiltered(
                                      imageFilter: ImageFilter.blur(
                                          sigmaX: 20, sigmaY: 20),
                                      enabled: true,
                                      child: Container(
                                        decoration:
                                            BoxDecoration(color: colorFEFEFE),
                                        child: Text(
                                          userEntity.getLiveIn(),
                                          maxLines: 1,
                                          overflow: TextOverflow.ellipsis,
                                          style: lfTextStyle(
                                              sp: 13.sp,
                                              fontFamily: fontMedium,
                                              color: colorFEFEFE),
                                        ),
                                      ),
                                    ),
                                  )
                                : Text(
                                    userEntity.getLiveIn(),
                                    maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                    textWidthBasis: TextWidthBasis.longestLine,
                                    style: lfTextStyle(
                                        sp: 13.sp,
                                        fontFamily: fontMedium,
                                        color: Colors.white),
                                  ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.topRight,
            child: userEntity.isNew > 0 && showNew
                ? Container(
                    margin:
                        EdgeInsets.symmetric(horizontal: 10.w, vertical: 6.h),
                    padding:
                        EdgeInsets.symmetric(horizontal: 5.w, vertical: 1.h),
                    decoration: BoxDecoration(
                        color: colorFF007F, borderRadius: 10.r.radius),
                    child: Text(
                      'new',
                      style: TextStyle(fontSize: 12.sp, color: Colors.white),
                    ),
                  )
                : 1.h.sbw,
          )
        ],
      ),
    );
  }

  Color _bottomColor() {
    if (needBlur) {
      return Colors.transparent;
    }
    if (userEntity.avatar == null ||
        userEntity.avatar!.trim().isEmpty ||
        userEntity.avatar!.contains('gender')) {
      return color50F2A7BE;
    }
    return Colors.transparent;
  }
}
